#!/bin/bash

#
#  Filename: createSolution.sh
#  Author  : lukas singer  lukas42singer<at>gmail.com
#
#  Comment : creates a template for project euler solutions in python
#            give the number of the problem as command line parameter or
#            give it when you get prompted
#            the generated file is executable for the user who runs this script
#
#  Changes : 17.1.2013 - changed editor to vim, if MY_LIBRARY is set then its
#            value will get opened in a second tab.
#            (unset MY_LIBRARY if not using vim!)
#
#

###########################
#####  CONFIGURATION  #####
MY_PYTHON="python3.3"
MY_AUTHOR="lukas singer"
MY_EDITOR=vim
#set MY_LIBRARY to open in second tab when using vim, comment it otherwise!
MY_LIBRARY="euler.py"
MY_PREFIX="e"
MY_SUFFIX=".py"
MY_USESVN=True
###########################

###########################
##### CHECK/GET PARAM #####
if [ "$#" -lt 1 ]; then
  echo "which problem?"
  read MY_NUM
else
  MY_NUM=$1
fi
###########################

###########################
#### CHECK/CREATE FILE ####
MY_FILENAME=$MY_PREFIX$MY_NUM$MY_SUFFIX
if [ -e $MY_FILENAME ]; then
  echo $MY_FILENAME" allready exists!"
else
  touch $MY_FILENAME
  chmod u+x $MY_FILENAME
  if [ $MY_USESVN ]; then
    svn add $MY_FILENAME
    svn propset svn:executable ON $MY_FILENAME
  fi
  echo "#!/usr/bin/env "$MY_PYTHON >> $MY_FILENAME
  echo "" >> $MY_FILENAME
  echo "#" >> $MY_FILENAME
  echo "#  Filename: "$MY_FILENAME >> $MY_FILENAME
  echo "#  Author  : "$MY_AUTHOR >> $MY_FILENAME
  echo "#" >> $MY_FILENAME
  echo "#  Comment : Created by createSolution.sh" >> $MY_FILENAME
  echo "#" >> $MY_FILENAME
  echo "" >> $MY_FILENAME
  echo "#import euler" >> $MY_FILENAME
  echo "#from euler import *" >> $MY_FILENAME
  echo "" >> $MY_FILENAME
  echo "def pe"$MY_NUM"():" >> $MY_FILENAME
  echo "  " >> $MY_FILENAME
  echo "" >> $MY_FILENAME
  echo 'if __name__=="__main__":' >> $MY_FILENAME
  echo "  pe"$MY_NUM"()" >> $MY_FILENAME
  echo "" >> $MY_FILENAME
  if [ -e $MY_LIBRARY ]; then
    $MY_EDITOR -p  $MY_FILENAME $MY_LIBRARY
  else
    $MY_EDITOR $MY_FILENAME
  fi
fi
###########################
